﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
namespace UI.ViewModel
{
    [PropertyChanged.AddINotifyPropertyChangedInterface]
   public class RepairViewModel:ViewModelBase
    {

        Model.DataContext context = new Model.DataContext();


        public List<Model.R_Repair> ListRepairs { get; set; }
        public List<Model.R_RepairType> ListRepairType { get; set; }




        public Model.R_Repair CurrRepair { get; set; }




        public List<Model.Building> Buildings { get; set; }

        public List<Model.Rooms> Rooms { get; set; }




        public HandyControl.Controls.Dialog Dialog { get; set; }













        /// <summary>
        /// 获取所有
        /// </summary>
        public async void GetAll()
        {
            ListRepairs =await  context.DB.Queryable<Model.R_Repair>().ToListAsync();
        }

        public async void GetDaiBan()
        {
            ListRepairs = await context.DB.Queryable<Model.R_Repair>().Where(r => r.State=="待维修").ToListAsync();
        }

        public async void GetYiBan()
        {
            ListRepairs = await context.DB.Queryable<Model.R_Repair>().Where(r => r.State == "维修中").ToListAsync();
        }

        public async void GetHuifang()
        {
            ListRepairs = await context.DB.Queryable<Model.R_Repair>().Where(r => r.State == "已维修").ToListAsync();
        }

        public async Task<List<Model.R_Repair>> Query(string  RId ,DateTime? startTime ,DateTime? endTime ,string state =null)
        {
            var query = context.DB.Queryable<Model.R_Repair>();
            if(!string.IsNullOrEmpty(RId))
            {
               query =   query.Where(r=>r.Id ==RId);
            }

            if(startTime!=null)
            {
                query = query.Where(r=>r.AppointmentTime > startTime);
            }
            if (endTime != null)
            {
                query = query.Where(r => r.AppointmentTime <= endTime);
            }

            if(!string.IsNullOrEmpty(state))
            {
                query = query.Where(r => r.State ==state);
            }

           return await query.ToListAsync();
        }

        public async void Save(Model.R_Repair r)
        {
            try
            {
               var t = await context.DB.Saveable<Model.R_Repair>(r).ExecuteCommandAsync();
               if(t>0)
                {
                    HandyControl.Controls.Growl.SuccessGlobal("保存成功！");
                }
            }
            catch (Exception ex)
            {

                HandyControl.Controls.Growl.WarningGlobal(ex.Message);
            }
           
        }


        public async void Delete (Model.R_Repair r)
        {
            await context.DB.Deleteable<Model.R_Repair>(r).ExecuteCommandAsync();
        }
    }
}
